#include <QAbstractButton>
#include <QApplication>
#include <QDesktopWidget>
#include "widget-helper.h"

bool WidgetHelper::equalizeWidgetWidth (const QList<QWidget*>& widgets,
                                        const QMap<QWidget*,QStringList>& alternativeTexts)
{
    if (widgets.isEmpty())
        return false;

    int maxWidth = 0;
    for (int i = 0; i < widgets.size(); i++)
    {
        int width = widgets.at(i)->sizeHint().width();
        if (maxWidth < width)
            maxWidth = width;
    }

    QMapIterator<QWidget*,QStringList> iterator(alternativeTexts);
    while (iterator.hasNext())
    {
        iterator.next();
        QWidget* widget = iterator.key();
        QStringList alternativeTexts = iterator.value();

        QLabel* label = qobject_cast<QLabel*>(widget);
        if (label)
        {
            WidgetHelperPrivate<QLabel>::setMaxWidgetWidth(label, alternativeTexts, maxWidth);
            continue;
        }

        QAbstractButton* button = qobject_cast<QAbstractButton*>(widget);
        if (button)
        {
            WidgetHelperPrivate<QAbstractButton>::setMaxWidgetWidth(button, alternativeTexts, maxWidth);
            continue;
        }
    }

    for (int i = 0; i < widgets.size(); i++)
        widgets.at(i)->setFixedWidth(maxWidth);

    return true;
}

void WidgetHelper::centerWidgetPosition (QWidget* widget)
{
    QWidget* parentWidget = widget->parentWidget();
    QRect widgetGeometry = widget->geometry();

    if (parentWidget)
        widgetGeometry.moveCenter(parentWidget->geometry().center());
    else
        widgetGeometry.moveCenter(QApplication::desktop()->availableGeometry(widget).center());

    widget->setGeometry(widgetGeometry);
}

void WidgetHelper::moveWidgetIntoDesktop (QWidget* widget)
{
    QPoint position = WidgetHelper::globalPosition(widget);
    QRect desktopGeometry = QApplication::desktop()->availableGeometry();
    QPoint newPosition = position;

    if (position.x() < desktopGeometry.left())       // is it too far on the left
        newPosition.setX(desktopGeometry.left());
    else if (position.x() + widget->frameGeometry().width() > desktopGeometry.right())      // is it too far on the right
        newPosition.setX(qMax(desktopGeometry.left(), desktopGeometry.right() - widget->frameGeometry().width()));

    if (position.y() < desktopGeometry.top())        // is it too far on the top
        newPosition.setY(desktopGeometry.top());
    else if (position.y() + widget->frameGeometry().height() > desktopGeometry.bottom())    // is it too far on the bottom
        newPosition.setY(qMax(desktopGeometry.top(), desktopGeometry.bottom() - widget->frameGeometry().height()));

    WidgetHelper::moveToGlobalPosition(newPosition, widget);
}

QPoint WidgetHelper::globalPosition (const QWidget* widget)
{
    if (widget->parent() == 0)
        return widget->pos();
    else
        return widget->mapToGlobal(widget->mapFromParent(widget->pos()));
}

void WidgetHelper::moveToGlobalPosition (const QPoint& position, QWidget* widget)
{
    if (widget->parent() == 0)
        widget->move(position);
    else
        widget->move(widget->mapToParent(widget->mapFromGlobal(position)));
}
